package com.sap.tpcd.policybuilder.policycontroller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.sap.tpcd.policybuilder.policymodel.Policy;

public class PolicyBuilder implements IPolicyBuilder {

	/*
	 * PolicyBuilder class that get an inputStream and then detect automatically
	 * the type of the policy inside the input Stream which is in general could
	 * be File
	 * 
	 * @see
	 * com.sap.tpcd.policybuilder.policycontroller.IPolicyBuilder#policyBuilder
	 * (java.io.InputStream)
	 */

	/*
	 * Detect automatically the type of the document (a firewall file , XACML
	 * file ...)
	 * 
	 * @see
	 * com.sap.tpcd.policybuilder.policycontroller#policyFileDetector(java.io
	 * .InputStream)
	 */
	protected String policyfileDetector(InputStream inputfile) {
		return null;
	}

	/*
	 * Convert an inputStream to a file to can get its properties like path ..
	 * 
	 * @see
	 * com.sap.tpcd.policybuilder.policycontroller#convertInputStreamTofile(
	 * java.io.InputStream)
	 */
	protected File convertInputStreamTofile(InputStream inputStream) {
		File f = new File("src/com/sap/tpcd/policybuilder/ressources/newfile.xml");
		try {

			// write the inputStream to a FileOutputStream
			FileOutputStream out = new FileOutputStream(f);
			int read = 0;
			byte[] bytes = new byte[1024];
			while ((read = inputStream.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}
			inputStream.close();
			out.flush();
			out.close();
			System.out.println(out.toString());
			System.out.println("New file created!");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return f;
	}

	/*
	 * Convert an File to an inputSteam ..
	 * 
	 * @see
	 * com.sap.tpcd.policybuilder.policycontroller#convertFileToInputStream(
	 * java.io.File)
	 */

	protected InputStream convertFileToInputStream(File f) {
		return null;
	}

	@Override
	public Policy policyBuilder(InputStream inputStream) {
		return null;
	}

	public static void main(String args[]) {	
		
	}

}
